

function unblock(emailId)
{
if (window.confirm(LOCID_WARN_HARMFUL_CONTENT))
{
var o = safeWindowOpen(prependOrgName("/_controls/emailbody/msgBody.aspx?unblockContent=1&id=" + emailId + "&entityType=email"), null, "width=700,height=500,menubar=1,status=1,resizable=1,scrollbars=1");

o.opener = null;
o.focus();
}
}


function ChooseTemplateTarget()
{

var sUrl = prependOrgName("/activities/email/dlg_template_target.aspx?");


var oSelectedId, oSelectedType;


var nObjects = 0;


var bConflicts = false;
var iId;

var arrUrlParts = new Array();



var RegardingItems = crmForm.all.regardingobjectid.Items();
if (RegardingItems.length > 0 && isValidTemplateObjectType(RegardingItems[0].type))
{
nObjects++;
var sUrlElement = "oid=" + CrmEncodeDecode.CrmUrlEncode(RegardingItems[0].id) +
"&otype=" + CrmEncodeDecode.CrmUrlEncode(RegardingItems[0].type) +
"&oname=" + CrmEncodeDecode.CrmUrlEncode(RegardingItems[0].name) + "&opart=re";

arrUrlParts[arrUrlParts.length] = sUrlElement;

oSelectedId = RegardingItems[0].id;
oSelectedType = RegardingItems[0].type;
iId = oSelectedId;
}


var ToRecipients = crmForm.all.to.Items();
for (var i = 0; i < ToRecipients.length; i++)
{
if (isValidTemplateObjectType(ToRecipients[i].type))
{
nObjects++;
var sUrlElement = "oid=" + CrmEncodeDecode.CrmUrlEncode(ToRecipients[i].id) +
"&otype=" + CrmEncodeDecode.CrmUrlEncode(ToRecipients[i].type) +
"&oname=" + CrmEncodeDecode.CrmUrlEncode(ToRecipients[i].name) + "&opart=to";

arrUrlParts[arrUrlParts.length] = sUrlElement;

oSelectedId = ToRecipients[i].id;
oSelectedType = ToRecipients[i].type;


if (!bConflicts)
{
if (IsNull(iId))
{
iId = oSelectedId;
}

bConflicts = (iId != oSelectedId);
}
}
}


if (!IsNull(crmForm.all.cc))
{
var CcRecipients = crmForm.all.cc.Items();
for (var i = 0; i < CcRecipients.length; i++)
{
if (isValidTemplateObjectType(CcRecipients[i].type))
{
nObjects++;
var sUrlElement = "oid=" + CrmEncodeDecode.CrmUrlEncode(CcRecipients[i].id) +
"&otype=" + CrmEncodeDecode.CrmUrlEncode(CcRecipients[i].type) +
"&oname=" + CrmEncodeDecode.CrmUrlEncode(CcRecipients[i].name) + "&opart=cc";
arrUrlParts[arrUrlParts.length] = sUrlElement;

oSelectedId = CcRecipients[i].id;
oSelectedType = CcRecipients[i].type;


if (!bConflicts)
{
if (IsNull(iId))
{
iId = oSelectedId;
}
bConflicts = (iId != oSelectedId);
}
}
}
}

if (nObjects == 0)
{
if(RegardingItems.length > 0 && RegardingItems[0].type==BulkOperation)
{
alert(LOCID_EMAIL_CHANGE_REGARDING);
}
else
{
alert(LOCID_EMAIL_RECIPIENT_TMPLT_RQD);
}
return null;
}

if (nObjects > 1 && bConflicts)
{

for( var i = 0; i < arrUrlParts.length; i++ )
{
if( sUrl.length + arrUrlParts[i].length > 2000 )
{
alert(LOCID_EMAIL_TOO_MANY_TARGETS);
break;
}
sUrl += ( i > 0 ?  "&" : "" ) +  arrUrlParts[i];
}

var sResponse = openStdDlg(sUrl, null, 600, 350);
if (sResponse == null)
return;

oSelectedId = sResponse.oid;
oSelectedType = sResponse.otype;
}

return new TemplateTarget(oSelectedId, oSelectedType);
}


function ApplyTemplate()
{
var oTarget = ChooseTemplateTarget();

if (oTarget == null)
return;

var sUrl = prependOrgName("/_grid/cmds/dlg_bulkemail.aspx?bulkemail=false" + "&objectTypeCode=" + oTarget.otype + "&objectId=" + oTarget.oid);
var sResponse = openStdDlg(sUrl, null, 600, 350);

if (sResponse != null)
{
crmForm.all.description.InsertValue( sResponse.EmailBody );

var sEmailSubject = crmForm.all.subject.DataValue;
if( !IsNull( sEmailSubject ) && sEmailSubject.length > 0 )
{
if( !confirm(LOCID_EMAIL_REPLACE_SUBJECT) )
{
return;
}
}



crmForm.all.subject.DataValue = CrmEncodeDecode.CrmHtmlDecode(sResponse.EmailSubject).substr(0, crmForm.all.subject.MaxLength);
}
}






function isValidTemplateObjectType(nObjectType)
{
return (nObjectType != Queue && nObjectType!=BulkOperation && nObjectType != UnresolvedEmailParty && !IsUserDefinedEntityObjectTypeCode(nObjectType));
}

function TemplateTarget( oid, otype)
{
this.oid = oid;
this.otype = otype;
}


function InsertKBArticle()
{
var sUrl = prependOrgName("/CS/dialogs/KBSearch.aspx?HideEmailButton=true");
var oLookupItems = openStdDlg(sUrl, null, 700, 500);

if (!IsNull(oLookupItems))
{
var oId = oLookupItems.items[0].id;
if (!IsNull(oId))
{
sUrl = prependOrgName("/CS/articles/viewer/content.aspx?id="+oId);
var oXmlHttp = CreateXmlHttp();
oXmlHttp.open("POST", sUrl, false);
oXmlHttp.send("");
if (oXmlHttp.responseText != null)
{
crmForm.all.description.InsertValue(oXmlHttp.responseText);
}
}
}
}
